package tests.read;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Line;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.LineInterface;

/*
 * 
 * Attempts to fetch all lines in a budget
 * 
 */

public class FetchLinesTest00
{
	Budget budget;
	BudgetFactory factory;
	Line line;

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        
        line = (Line) budget.createLine();
        line.setName("Test Line");
		budget.add(line);
		budget.commit();
        
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete( line, "Lines", WorkletContext.getInstance());
		Helper.delete( budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		try
		{
			ArrayList<LineInterface> lines = budget.fetchLines(null);
			
			if(lines.size() != 0)
			{
				if(!lines.get(0).getName().equals("Test Line"))
				{
					fail("Fetching all lines failed");
				}
			}
			else
			{
				fail("Fetching all lines failed");
			}
		}
		catch(Exception e)
		{
			System.err.println("FetchFailed");
			e.printStackTrace();
			
			fail("Fetching all lines failed with exception "+e);
		}
	}
}